package top.pengdev.esoffice.util;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import top.pengdev.esoffice.common.type.File;

import java.io.*;

/**
 * ReadOfficeContentUtil class
 * @author pengdev
 * @description 读取Office内容工具类
 * @date 2020-11-25 09:51
 **/
public class ReadOfficeContentUtil {
    private Logger logger = LoggerFactory.getLogger(ReadOfficeContentUtil.class);

    /**
     * 读入word内容
     * @param path 路径
     * @param input 输入流
     * @return 文件内容
     */
    public static String readWord(String path, InputStream input) {
        String buffer = "";
        try {
            if(path.endsWith(File.DOC)) {
                WordExtractor ex = new WordExtractor(input);
                buffer = ex.getText();
                ex.close();
            } else if(path.endsWith(File.DOCX)) {
                XWPFDocument doc = new XWPFDocument(input);
                XWPFWordExtractor ex = new XWPFWordExtractor(doc);
                buffer = ex.getText();
                ex.close();
                doc.close();
            }
            input.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * 读入Excel内容
     * @param path 路径
     * @param input 输入流
     * @return 文件内容
     */
    public static String readExcel(String path, InputStream input) {
        String buffer = "";
        try {
            if (path.endsWith(File.XLS)) {
                HSSFWorkbook wb = new HSSFWorkbook(input);
                ExcelExtractor extractor = new ExcelExtractor(wb);
                extractor.setIncludeSheetNames(true);
                extractor.setFormulasNotResults(true);
                extractor.setIncludeCellComments(true);
                buffer = extractor.getText();
                extractor.close();
                wb.close();
            } else if (path.endsWith(File.XLSX)) {
                XSSFWorkbook document = new XSSFWorkbook(input);
                POIXMLTextExtractor extractor = new XSSFExcelExtractor(document);
                buffer = extractor.getText();
                extractor.close();
                document.close();
            }
            input.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * 读入PPT内容
     * @param path 路径
     * @param input 输入流
     * @return 文件内容
     */
    public static String readPpt(String path, InputStream input) {
        String buffer = "";
        try {
            if(path.endsWith(File.PPT)) {

            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * 读入PDF内容
     * @param path 路径
     * @param input 输入流
     * @return 文件内容
     */
    public static String readPdf(String path, InputStream input) {
        String buffer = "";
        try {
            if(path.endsWith(File.PDF)) {
                PDDocument doc = PDDocument.load(input);
                PDFTextStripper stripper = new PDFTextStripper();
                buffer = stripper.getText(doc);
                doc.close();
                input.close();
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * 读入txt内容
     * @param path 路径
     * @param input 输入流
     * @return 文件内容
     */
    public static String readTxt(String path, InputStream input) {
        String buffer = "";
        try {
            if(path.endsWith(File.TXT)) {
                InputStreamReader isReader = new InputStreamReader(input);
                BufferedReader reader = new BufferedReader(isReader);
                String line = null;
                while((line = reader.readLine()) != null) {
                    buffer += line;
                }
                reader.close();
                isReader.close();
                input.close();
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * 读取上传文件内容
     * @param file
     * @return 文件内容
     * @throws IOException
     */
    public static String readContent(MultipartFile file) throws IOException {
        String path = file.getOriginalFilename();
        InputStream input = file.getInputStream();
        return readContent(path, input);
    }

    /**
     * 读取文件内容
     * @param file 需要读取的文件
     * @return 文件内容
     * @throws IOException IO异常
     */
    public static String readContent(java.io.File file) throws IOException {
        String path = file.getPath();
        InputStream input = new FileInputStream(file);
        return readContent(path, input);
    }

    /**
     * 读入文件内容
     * @param path 路径
     * @param input 输入流
     * @return 文件内容
     */
    public static String readContent(String path, InputStream input){
        String content = "";
        String suffix = path.substring(path.lastIndexOf("."));
        switch(suffix) {
            case File.DOC:
            case File.DOCX: content = readWord(path, input); break;
            case File.XLS:
            case File.XLSX: content = readExcel(path, input); break;
            case File.PPT:
            case File.PPTX: content = readPpt(path, input); break;
            case File.PDF: content = readPdf(path, input); break;
            case File.TXT: content = readTxt(path, input); break;
            default: break;
        }
        return content;
    }

    public static void main(String[] args) throws IOException {
        java.io.File file = new java.io.File("D:\\package\\PDF\\Linux.pdf");
        String content = readContent(file);
        System.out.println(content);
    }
}
